package tree.avl;

/**
 * @ author : HyoJoo-W
 * @ createDate : 2021-06-05 21:49
 * @ Description :
 */
public class AVLTreeDemo
{
    public static void main(String[] args)
    {
        //int[] array = {4, 3, 6, 5, 7, 8};
        int[] array = {10, 12, 8, 9, 7, 6};
        AVLTree avlTree = new AVLTree();

        for (int i : array)
        {
            avlTree.add(new Node(i));
        }

        System.out.println("中序遍历...");
        avlTree.midOrder();

        System.out.println("平衡处理后...");
        System.out.println("树的高度为: " + avlTree.getRoot().getHeight());
        System.out.println("左子树高度: " + avlTree.getRoot().getLeftHeight());
        System.out.println("右子树高度: " + avlTree.getRoot().getRightHeight());
        System.out.println(avlTree.getRoot().left);


    }
}

class AVLTree
{
    private Node root;

    public Node getRoot()
    {
        return root;
    }

    public void add(Node node)
    {
        if (root == null)
        {
            root = node;
        }
        else
        {
            root.add(node);
        }
    }

    public void midOrder()
    {
        if (root != null)
        {
            root.midOrder();
        }
        else
        {
            System.out.println("根节点为空");
        }
    }

    public Node search(int value)
    {
        if (root == null)
        {
            return null;
        }
        else
        {
            return root.search(value);
        }
    }

    public Node searchParent(int value)
    {
        if (root == null)
        {
            return null;
        }
        else
        {
            return root.searchParent(value);
        }
    }

    //删除右子树的最小值
    public int delRightTreeMin(Node node)
    {
        Node target = node;
        while (target.left != null)
        {
            target = target.left;
        }
        //此使target指向最小值节点
        delNode(target.value);

        return target.value;
    }

    public void delNode(int value)
    {
        if (root == null)
        {
            return;
        }
        else
        {
            Node target = search(value);
            if (target == null)
            {
                return;
            }
            if (root.left == null && root.right == null)
            {
                root = null;
            }

            Node parent = searchParent(value);

            //开始删除...
            if (target.left == null && target.right == null)
            {
                //叶子节点
                if (parent.left != null && parent.left.value == value)
                {
                    parent.left = null;
                }
                else if (parent.right != null && parent.right.value == value)
                {
                    parent.right = null;
                }
            }
            else if (target.left != null && target.right != null)
            {
                //两颗子树
                target.value = delRightTreeMin(target.right);
            }
            else
            {
                //要删除的节点只有一颗子树
                if (target.left != null)
                {
                    //要删除的节点只有左子树
                    if (parent != null)
                    {
                        //要删除的节点有父节点
                        if (parent.left.value == value)
                        {
                            //要删除的节点是parent的左子节点
                            parent.left = target.left;
                        }
                        else
                        {
                            //要删除的节点是parent的右子节点
                            parent.right = target.left;
                        }
                    }
                    else
                    {
                        //要删除的节点没有父节点,也就是根节点
                        root = target.left;
                    }

                }
                else
                {
                    if (parent != null)
                    {
                        //要删除的节点只有一颗右子树
                        if (parent.left.value == value)
                        {
                            //要删除的节点是父节点的左子节点
                            parent.left = target.right;
                        }
                        else
                        {
                            //要删除的节点是parent的右子节点
                            parent.right = target.right;
                        }
                    }
                    else
                    {
                        root = target.right;
                    }

                }

            }
        }
    }
}

class Node
{
    public int value;
    public Node left;
    public Node right;

    public Node(int value)
    {
        this.value = value;
    }

    //计算左子树高度
    public int getLeftHeight()
    {
        return left == null ? 0 : left.getHeight();
    }

    //计算右子树高度
    public int getRightHeight()
    {
        return right == null ? 0 : right.getHeight();
    }

    //计算以该节点为根节点的树的高度
    public int getHeight()
    {
        return Math.max(left == null ? 0 : left.getHeight(), right == null ? 0 : right.getHeight()) + 1;
    }

    //左旋转
    private void leftRotate()
    {
        //1.
        Node newNode = new Node(value);

        //2.
        newNode.left = left;

        //3.
        newNode.right = right.left;

        //4.
        value = right.value;

        //5.
        right = right.right;

        //6.
        left = newNode;

    }

    //右旋转
    public void rightRotate()
    {
        Node newNode = new Node(value);
        newNode.right = right;
        newNode.left = left.right;
        value = left.value;
        left = left.left;
        right = newNode;
    }

    @Override
    public String toString()
    {
        return "Node{" +
                "value=" + value +
                '}';
    }

    public void add(Node node)
    {
        if (node == null)
        {
            return;
        }
        if (node.value < this.value)
        {
            if (this.left == null)
            {
                this.left = node;
            }
            else
            {
                this.left.add(node);
            }
        }
        else
        {
            if (this.right == null)
            {
                this.right = node;
            }
            else
            {
                this.right.add(node);
            }
        }

        //添加一个节点后,如果右子树高度与左子树高度差值 > 1,则平衡左旋转
        if (getRightHeight() - getLeftHeight() > 1)
        {
            //双旋转判断
            if (right != null && right.getRightHeight() < right.getLeftHeight())
            {
                //当前节点右子节点右旋
                right.rightRotate();
            }
            leftRotate();
            return;//!!!
        }
        if (getLeftHeight() - getRightHeight() > 1)
        {
            if (left != null && left.getRightHeight() > left.getLeftHeight())
            {
                //当前节点左子节点左旋
                left.leftRotate();
            }
            //当前节点右旋
            rightRotate();
        }
    }

    //中序遍历
    public void midOrder()
    {
        if (this.left != null)
        {
            this.left.midOrder();
        }

        System.out.println(this);

        if (this.right != null)
        {
            this.right.midOrder();
        }
    }

    public Node search(int value)
    {
        if (value == this.value)
        {
            return this;
        }
        else if (value < this.value)
        {
            if (this.left != null)
            {
                return this.left.search(value);
            }
            else
            {
                return null;
            }
        }
        else
        {
            if (this.right != null)
            {
                return this.right.search(value);
            }
            else
            {
                return null;
            }
        }
    }

    public Node searchParent(int value)
    {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value))
        {
            return this;
        }
        else
        {
            if (value < this.value && this.left != null)
            {
                return this.left.searchParent(value);
            }
            else if (value >= this.value && this.right != null)
            {
                return this.right.searchParent(value);
            }
            else
            {
                //根节点,left = null,right = null
                return null;
            }
        }
    }
}